package com.lighting.smssystem.utils.mybatis;

import com.lighting.smssystem.constrants.UserRole;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Author LeeMaster
 */
public class UserRoleTypeHandler extends BaseTypeHandler<UserRole> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, UserRole parameter, JdbcType jdbcType) throws SQLException {
        System.out.println("查询参数" + parameter);
        if (parameter != null){
            ps.setInt(i,parameter.code);
        }else{
            ps.setInt(i,UserRole.COMMON.code);
        }
    }

    @Override
    public UserRole getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return getRole(rs.getInt(columnName));
    }

    @Override
    public UserRole getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return getRole(rs.getInt(columnIndex));
    }

    @Override
    public UserRole getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return getRole(cs.getInt(columnIndex));
    }


    private UserRole getRole(int role){
        switch (role){
            case 1:
                return UserRole.VIP;
            case 2:
                return UserRole.ADMIN;
            default:
                return UserRole.COMMON;
        }
    }
}
